/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sita.friend.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.friend.utils.MessageUtils;
import com.sita.tboard.ActivityBase;

import butterknife.Bind;
import de.greenrobot.event.EventBus;

/**
 * 小组列表页
 */
public class FriendPublicGroupListActivity extends ActivityBase {

    private static final String TAG = FriendPublicGroupListActivity.class.getSimpleName();

    protected ListView listView;
    protected InputMethodManager inputMethodManager;
    private View root;
    @Bind(R.id.img_back)
    public ImageView img_back;
    private TextView tvPublicGroupUnreadMessageNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_fragment_public_group_list);
        this.inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        this.initView();
        this.setUpView();
    }

    protected void initView() {
        RelativeLayout myPublicGroup = (RelativeLayout) findViewById(R.id.rl_friend_my_public_group);
        myPublicGroup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2016/4/13 goto my public group list page
            }
        });
        ImageView imgBack = (ImageView) root.findViewById(R.id.img_back);
        imgBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getParent().finish();
            }
        });

        tvPublicGroupUnreadMessageNumber = (TextView) root.findViewById(R.id.friend_my_public_group_unread_msg_number);
        tvPublicGroupUnreadMessageNumber.setVisibility(View.INVISIBLE);
    }


    public void refreshUnreadPublicGroupMessage() {
        if (tvPublicGroupUnreadMessageNumber == null) {
            return;
        }
        int count = MessageUtils.getUnreadInviteMessageCount();
        if (count > 0) {
            tvPublicGroupUnreadMessageNumber.setText(String.valueOf(count));
            tvPublicGroupUnreadMessageNumber.setVisibility(View.VISIBLE);
        }else {
            tvPublicGroupUnreadMessageNumber.setText(String.valueOf(count));
            tvPublicGroupUnreadMessageNumber.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 获取推荐小组列表，并过滤掉黑名单和排序
     */
    protected void getRecommendPublicGroupList() {
    }

    protected void setUpView2() {

    }

    protected void hideSoftKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    protected void setUpView() {

//        //设置联系人数据
//        setContactsMap(FriendHelper.getInstance().getContactList());

        setUpView2();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onResume() {
        // TODO: 2016/3/4 no need to reset up view every time
        setUpView();
        refreshUnreadPublicGroupMessage();
        super.onResume();
    }

    @Override
    public void onStart() {
        EventBus.getDefault().register(this);
        super.onStart();
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }
}
